package app.android.com.wuandroidapp.test.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.db.user.entity.PassWord;

import java.security.PrivateKey;
import java.security.PublicKey;
import java.util.List;

import app.android.com.utilscode.util.Base64Utils;
import app.android.com.utilscode.util.EmptyUtils;
import app.android.com.utilscode.util.RSAUtils;
import app.android.com.utilscode.util.ScreenUtils;
import app.android.com.utilscode.util.ToastUtils;
import app.android.com.wuandroidapp.AndroidApplication;
import app.android.com.wuandroidapp.AppConstants;
import app.android.com.wuandroidapp.R;
import app.android.com.wuandroidapp.library.activity.BaseNoTitleActivity;
import app.android.com.wuandroidapp.test.model.db.dao.PasswordUtils;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class SettingPWActivity extends BaseNoTitleActivity {

    @BindView(R.id.title_text)
    TextView titleText;
    @BindView(R.id.iv_title_right)
    ImageView ivTitleRight;
    @BindView(R.id.title)
    RelativeLayout title;
    @BindView(R.id.old_password)
    EditText oldPassword;
    @BindView(R.id.old_password_ll)
    LinearLayout oldPasswordLl;
    @BindView(R.id.line_1)
    View line1;
    @BindView(R.id.new_password)
    EditText newPassword;
    @BindView(R.id.confirm_password)
    EditText confirmPassword;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_settings_pw);
        ButterKnife.bind(this);

        titleText.setText("设置密码");
        if (AndroidApplication.passwordUtils.listAll().size() > 0) {
            oldPasswordLl.setVisibility(View.VISIBLE);
        } else {
            oldPasswordLl.setVisibility(View.GONE);
        }
        ivTitleRight.setImageResource(R.mipmap.select_on);
    }

    public void back(View v) {
        finish();
    }


    @OnClick(R.id.iv_title_right)
    public void onViewClicked() {
        String newpw = newPassword.getText().toString();
        String confirmPw = confirmPassword.getText().toString();
        String oldpw = oldPassword.getText().toString();
        if (AndroidApplication.passwordUtils.listAll().size() > 0) {
            List<PassWord> passWordList = AndroidApplication.passwordUtils.listAll();
            PassWord passWord = passWordList.get(0);
            String pd = passWord.getPassword();
            if (EmptyUtils.isEmpty(oldpw)) {
                ToastUtils.showShort("旧密码不能为空");
                return;
            }
            pd = getsrsajia(pd);
            if (!pd.equals(oldpw)) {
                ToastUtils.showShort("旧密码错误");
                return;
            } else {
                if (EmptyUtils.isEmpty(newpw)) {
                    ToastUtils.showShort("新密码不能为空");
                    return;
                }
                if (EmptyUtils.isEmpty(confirmPw)) {
                    ToastUtils.showShort("重复密码不能为空");
                    return;
                }
                if (newpw.equals(confirmPw)) {
                    Toast.makeText(this, "Ok", Toast.LENGTH_LONG).show();
                    confirmPw = getrsawall(confirmPw);
                    passWord.setPassword(confirmPw);
                    AndroidApplication.passwordUtils.updateStudent(passWord);
                    oldPassword.setText("");
                    newPassword.setText("");
                    confirmPassword.setText("");
                } else {
                    ToastUtils.showShort("两次输入的密码不一致");
                }
            }
        } else {

            if (EmptyUtils.isEmpty(newpw)) {
                ToastUtils.showShort("密码不能为空");
                return;
            }
            if (EmptyUtils.isEmpty(confirmPw)) {
                ToastUtils.showShort("密码不能为空");
                return;
            }
            if (newpw.equals(confirmPw)) {
                Toast.makeText(this, "Ok", Toast.LENGTH_LONG).show();
                PassWord passWord = new PassWord();
                confirmPw = getrsawall(confirmPw);
                passWord.setPassword(confirmPw);
                AndroidApplication.passwordUtils.insertStudent(passWord);
                newPassword.setText("");
                confirmPassword.setText("");
            } else {
                ToastUtils.showShort("两次输入的密码不一致");
            }
        }
    }
 //mX5b7f04ZkCwLGuzq+OXsfk5wibU8MTTMLLYFMHtOHSsyKZLoeAWP4DDikqfo9DBxHeAcShYXTuFNkIqsGbwsnQdxvtNoKu0EVBexFflwp57laIZhhokTHBbEZFns7hXG4uvIQ/4xt1Cm0MXfc4QqIguO1vauEiN2rUYvAfxu/Y=
//mX5b7f04ZkCwLGuzq+OXsfk5wibU8MTTMLLYFMHtOHSsyKZLoeAWP4DDikqfo9DBxHeAcShYXTuFNkIqsGbwsnQdxvtNoKu0EVBexFflwp57laIZhhokTHBbEZFns7hXG4uvIQ/4xt1Cm0MXfc4QqIguO1vauEiN2rUYvAfxu/Y=
    public static String getrsawall(String source) {
        try {
            // 从字符串中得到公钥
            PublicKey publicKey = RSAUtils.loadPublicKey(AppConstants.WALL_RSA_KSY);
            // 从文件中得到公钥
            //      InputStream inPublic = getResources().getAssets().open("rsa_public_key.pem");
            //       PublicKey publicKey = RSAUtils.loadPublicKey(inPublic);
            // 加密
            byte[] encryptByte = RSAUtils.encryptData(source.getBytes(), publicKey);
            // 为了方便观察吧加密后的数据用base64加密转一下，要不然看起来是乱码,所以解密是也是要用Base64先转换
            if (encryptByte != null) {
                String afterencrypt = Base64Utils.encode(encryptByte);
                System.out.print(afterencrypt);
                return afterencrypt;
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }


    public static String getsrsajia(String source) {
        try {
            // 从字符串中得到私钥
            PrivateKey privateKey = RSAUtils.loadPrivateKey(AppConstants.RSA_KSY_PRI);
            // 解密
            //      InputStream inPublic = getResources().getAssets().open("rsa_public_key.pem");
            //       PublicKey publicKey = RSAUtils.loadPublicKey(inPublic);
           byte[] so= Base64Utils.decode(source);
            byte[] decryptByte = RSAUtils.decryptData(so, privateKey);

            String decryptStr = new String(decryptByte);
            System.out.print(decryptStr);
            return decryptStr;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
